import { defineStore } from 'pinia'
import { login, getUserInfo } from '@/api/auth/user'
import Cookies from 'js-cookie'
import router from '@/router'

interface UserState {
  token: ''
  userInfo: any
}

const useUserStore = defineStore('user', {
  state: (): UserState => ({
    token: '',
    userInfo: {}
  }),
  getters: {},
  actions: {
    async setToken(account: object) {
      const result = await login(account)
      this.token = result.data
      Cookies.set('lhytoken', this.token, {
        //一小时过期
        expires: new Date(new Date().getTime() + 3600 * 1000)
      })
      const userInfoResult = await getUserInfo()
      const userInfo = userInfoResult.data
      this.userInfo = userInfo
      router.push('/home/index')
    },
    async setUserInfo() {
      const userInfoResult = await getUserInfo()
      const userInfo = userInfoResult.data
      this.userInfo = userInfo
    }
  }
})

export default useUserStore
